package com.atguigu.lease.web.app.service.impl;

import com.atguigu.lease.common.context.LoginUserContext;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.model.enums.LeaseStatus;
import com.atguigu.lease.web.app.mapper.*;
import com.atguigu.lease.web.app.service.ApartmentInfoService;
import com.atguigu.lease.web.app.service.BrowsingHistoryService;
import com.atguigu.lease.web.app.service.GraphInfoService;
import com.atguigu.lease.web.app.service.RoomInfoService;
import com.atguigu.lease.web.app.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.app.vo.attr.AttrValueVo;
import com.atguigu.lease.web.app.vo.fee.FeeValueVo;
import com.atguigu.lease.web.app.vo.graph.GraphVo;
import com.atguigu.lease.web.app.vo.room.RoomDetailVo;
import com.atguigu.lease.web.app.vo.room.RoomItemVo;
import com.atguigu.lease.web.app.vo.room.RoomQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-26 11:12:39
 */
@Service
@Slf4j
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {
    @Autowired
    private RoomInfoMapper roomInfoMapper;
    @Autowired
    private BrowsingHistoryService browsingHistoryService;
    @Autowired
    private GraphInfoService graphInfoService;
    @Autowired
    private FacilityInfoMapper facilityInfoMapper;
    @Autowired
    private AttrValueMapper attrValueMapper;
    @Autowired
    private LabelInfoMapper labelInfoMapper;
    @Autowired
    private PaymentTypeMapper paymentTypeMapper;
    @Autowired
    private FeeValueMapper feeValueMapper;
    @Autowired
    private LeaseTermMapper leaseTermMapper;
    @Autowired
    private LeaseAgreementMapper leaseAgreementMapper;
    @Autowired
    private ApartmentInfoService apartmentInfoService;



    @Override
    public Page<RoomItemVo> pageRoomItemVo(Page<RoomItemVo> page, RoomQueryVo queryVo) {
        return roomInfoMapper.pageRoomItemVo(page,queryVo);
    }

    @Override
    public RoomDetailVo getRoomDetailVoById(Long id) {
        RoomDetailVo roomDetailVo = new RoomDetailVo();
        RoomInfo roomInfo = roomInfoMapper.selectById(id);
        if (ObjectUtils.isEmpty(roomInfo)){
            return null;
        }
        BeanUtils.copyProperties(roomInfo,roomDetailVo);
        // 查询公寓信息
        Long apartmentId = roomInfo.getApartmentId();
        ApartmentItemVo apartmentItemVo=apartmentInfoService.selectApartmentItemVoByApartmentId(apartmentId);
        roomDetailVo.setApartmentItemVo(apartmentItemVo);
        // 查询图片列表
        List<GraphVo> graphVoListByRoomId = getGraphVoListByRoomId(id);
        roomDetailVo.setGraphVoList(graphVoListByRoomId);
        // 插入属性信息列表
        List<AttrValueVo> attrValueVoList=attrValueMapper.selectAttrValueVoListByRoomId(id);
        roomDetailVo.setAttrValueVoList(attrValueVoList);
        //插入配套信息列表
        List<FacilityInfo> facilityInfos=facilityInfoMapper.selectFacilityInfoListByRoomId(id);
        roomDetailVo.setFacilityInfoList(facilityInfos);
        //插入标签信息列表
        List<LabelInfo> labelInfoList=labelInfoMapper.selectLabelInfoListByRoomId(id);
        roomDetailVo.setLabelInfoList(labelInfoList);
        // 插入支付方式列表
        List<PaymentType> paymentTypeList=paymentTypeMapper.selectPaymentTypeByRoomId(id);
        roomDetailVo.setPaymentTypeList(paymentTypeList);
        // 插入杂费列表
        List<FeeValueVo> feeValueVoList=feeValueMapper.selectFeeValueVoByApartmentId(apartmentId);
        roomDetailVo.setFeeValueVoList(feeValueVoList);
        // 插入租期列表
        List<LeaseTerm> leaseTerms=leaseTermMapper.selectLeaseTermListByRoomId(id);
        roomDetailVo.setLeaseTermList(leaseTerms);
        //插入房间是否被删除
        roomDetailVo.setIsDelete(roomInfo.getIsDeleted()==1);
        // 查询房间入住状态
        LambdaQueryWrapper<LeaseAgreement> leaseAgreementQueryWrapper = new LambdaQueryWrapper<>();
        leaseAgreementQueryWrapper.eq(LeaseAgreement::getRoomId,id);
        leaseAgreementQueryWrapper.in(LeaseAgreement::getStatus,LeaseStatus.SIGNED,LeaseStatus.WITHDRAWING,LeaseStatus.RENEWING);
        Long count = leaseAgreementMapper.selectCount(leaseAgreementQueryWrapper);
        roomDetailVo.setIsCheckIn(count>0);
        browsingHistoryService.saveHistory(LoginUserContext.getUser().getUserId(),id);
        return roomDetailVo;
    }

    @Override
    public Page<RoomItemVo> pageRoomItemVoByApartmentId(Page<RoomItemVo> roomItemVoPage, Long id) {
        return roomInfoMapper.pageRoomItemVoByApartmentId(roomItemVoPage,id);
    }


    // 查询图片列表
    @Override
    public List<GraphVo> getGraphVoListByRoomId(Long roomId) {
        LambdaQueryWrapper<GraphInfo> graphQueryWrapper = new LambdaQueryWrapper<>();
        graphQueryWrapper.eq(GraphInfo::getItemId,roomId).eq(GraphInfo::getItemType,ItemType.ROOM);
        List<GraphVo> graphVoList = new ArrayList<>();
        graphInfoService.list(graphQueryWrapper).forEach(graphInfo -> {
            GraphVo graphVo=new GraphVo();
            BeanUtils.copyProperties(graphInfo,graphVo);
            graphVoList.add(graphVo);
        });
        return graphVoList;
    }
}




